(function($){
    function initProvince() {
        var data = SysArea.province();
        var box = $('#filterProvince');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i] +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initCity(p) {
        var data = SysArea.city(p);
        var box = $('#filterCity');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i] +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initDistrict(p, c) {
        var data = SysArea.city(p, c);
        var box = $('#filterDistrict');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i][0] +'">'+ data[i][1] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initArea() {
        var pObj = $('#filterProvince');
        var cObj = $('#filterCity');
        var dObj = $('#filterDistrict');
        var p = pObj.attr('dvalue');
        var c = cObj.attr('dvalue');
        var d = dObj.attr('dvalue');
        initProvince();
        if(!!p) {
            pObj.find('option').each(function(){
                var o = $(this);
                if(o.attr('value') == p) {
                    o.attr('selected', 'selected');
                }
            });
            initCity(p);
        }
        if(!!c) {
            cObj.find('option').each(function(){
                var o = $(this);
                if(o.attr('value') == c) {
                    o.attr('selected', 'selected');
                }
            });
            initDistrict(p, c);
        }
        if(!!d) {
            dObj.find('option').each(function(){
                var o = $(this);
                if(o.attr('value') == d) {
                    o.attr('selected', 'selected');
                }
            });
        }
        pObj.on('change', function(){
            var o = $(this);
            initCity(o.val());
        });
        cObj.on('change', function(){
            var o = $(this);
            initDistrict($('#filterProvince').val(), o.val());
        });
    }
    function initSetState() {
        $('.but_set_state').on('click', function(){
            var obj = $(this);
            var state = obj.attr('dstate');
            var id = obj.attr('did');
            var url = obj.attr('durl');
            var info = '删除';
            switch(state) {
                case '-1':
                    info = '删除';
                    break;
                case '0':
                    info = '恢复';
                    break;
                case '1':
                    info = '发布';
                    break;
            }
            layer.confirm(
                '确定要【' + info + '】此用户吗？',
                {
                    icon: 3,
                    title: '系统提示',
                    btn: ['确 定', '取 消']
                },
                function(index){
                    layer.close(index);
                    layer.load();
                    $.post(
                        url,
                        {id: id, state: state},
                        function(data){
                            if(data.status == 10000) {
                                location.reload(true);
                            } else {
                                layer.closeAll();
                                layer.alert(data.errmsg);
                            }
                        },
                        'json'
                    );
                },
                function(index){
                    layer.close(index);
                }
            );
        });
    }
    function initSetExpert(_this) {
            var obj = _this;
            var value = obj.attr('dvalue');
            var id = obj.attr('did');
            var url = obj.attr('durl');
            var info = '';
            switch(value) {
                case '0':
                    info = '取消此用户专家身份';
                    break;
                case '1':
                    info = '设置此用户为专家';
                    break;
            }
            layer.confirm(
                '确定要【' + info + '】？',
                {
                    icon: 3,
                    title: '系统提示',
                    btn: ['确 定', '取 消']
                },
                function(index){
                    layer.close(index);
                    layer.load();
                    $.post(
                        url,
                        {id: id, expert: value},
                        function(data){
                            if(data.status == 10000) {
                                location.reload(true);
                            } else {
                                layer.closeAll();
                                layer.alert(data.errmsg);
                            }
                        },
                        'json'
                    );
                },
                function(index){
                    layer.close(index);
                }
            );
    }
    function initUpdatePassword() {
        $('.but_update_password').on('click', function(){
            var obj = $(this);
            var id = obj.attr('did');
            var url = obj.attr('durl');
            layer.prompt({
                title: '输入新密码，并确认',
                formType: 0 //0（文本）默认1（密码）2（多行文本
            }, function(pass){
                if(!pass || pass.length < 6) {
                    layer.alert('密码长度必须大于6位！')
                    return;
                }
                layer.load();
                $.post(
                        url,
                        {id: id, pwd: pass},
                        function(data){
                            if(data.status == 10000) {
                                layer.closeAll();
                                layer.alert('修改成功！');
                            } else {
                                layer.closeAll();
                                layer.alert(data.errmsg);
                            }
                        },
                        'json'
                );
            });
        });
    }
    $(function(){
        layer.config({
            extend: 'extend/layer.ext.js'
        });
        initArea();
        initSetState();
        //initSetExpert();
        initUpdatePassword();
        $(".but_set_expert").click(function(){
        	var _this=$(this);
        	var state = _this.attr("dvalue");
        	var url=_this.attr("dinfourl");
        	var title=_this.attr("dtitle");
        	var userId=_this.attr("did");
        	url = url + "?userid=" + userId;
        	if(state == 0){
        		initSetExpert($(this));
        	}else{
	        	layer.open({
	                type: 2,
	                area: ['1000px', '530px'],
	                title:title,
	                fix: false, //不固定
	                maxmin: true,
	                content: url
	            });
        	}
        });
        $('#activity_time').daterangepicker({
            "autoUpdateInput": false,
            "showDropdowns": true,
            "timePicker": true,
            "timePicker24Hour": true,
            "timePickerIncrement": 1,
            "autoApply": true,
            "locale": {
                "format": "YYYY-MM-DD HH:mm",
                "separator": " 至 ",
                "applyLabel": "确 定",
                "cancelLabel": "取 消",
                "fromLabel": "From",
                "toLabel": "To",
                "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
                "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                "firstDay": 1
            },
            "opens": "right",
            "drops": "down",
            "buttonClasses": "btn btn-sm",
            "applyClass": "btn-success",
            "cancelClass": "btn-default"
        }, function(start, end, label) {

        });

        $('#activity_time').on('apply.daterangepicker', function(ev, picker) {
            $(this).val(picker.startDate.format('YYYY-MM-DD HH:mm') + ' 至 ' + picker.endDate.format('YYYY-MM-DD HH:mm'));
        });
        $('#activity_time').on('cancel.daterangepicker', function(ev, picker) {
            $(this).val('');
        });
    });
})(jQuery);